<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_order_info".
 *
 * @property int $oid
 * @property int $tid
 * @property int $aid
 * @property string $goods_name
 * @property string $order_number
 * @property string $refund_number
 * @property int $goods_num
 * @property string $pay_type
 * @property int $order_amount
 * @property int $real_pay
 * @property int $order_status
 * @property string $is_write_off
 * @property string $is_finish
 * @property string $self_status
 * @property string $from
 * @property string $pay_note
 * @property string $pay_time
 * @property string $create_time
 */
class OrderInfo extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_order_info';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['oid', 'tid', 'aid', 'goods_name', 'order_number'], 'required'],
            [['oid', 'tid', 'aid', 'goods_num', 'order_amount', 'real_pay', 'order_status'], 'integer'],
            [['is_write_off', 'is_finish', 'self_status','pay_number'], 'string'],
            [['pay_time', 'create_time'], 'safe'],
            [['goods_name', 'from'], 'string', 'max' => 64],
            [['order_number', 'refund_number', 'pay_type'], 'string', 'max' => 32],
            [['pay_note'], 'string', 'max' => 1024],
            [['oid'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'oid' => 'Oids',
            'tid' => 'Tid',
            'aid' => 'Aid',
            'goods_name' => 'Goods Name',
            'order_number' => 'Order Number',
            'refund_number' => 'Refund Number',
            'goods_num' => 'Goods Num',
            'pay_type' => 'Pay Type',
            'order_amount' => 'Order Amount',
            'real_pay' => 'Real Pay',
            'order_status' => 'Order Status',
            'is_write_off' => 'Is Write Off',
            'is_finish' => 'Is Finish',
            'self_status' => 'Self Status',
            'from' => 'From',
            'pay_note' => 'Pay Note',
            'pay_time' => 'Pay Time',
            'create_time' => 'Create Time',
        ];
    }


    public function getDetailes()
    {
        $data = $this->hasOne(OrderDetails::className(),['oid' => 'oid']);
        return $data;
    }

    public function getDetails()
    {
        $data = $this->hasMany(OrderDetails::className(),['oid' => 'oid']);
        return $data;
    }

    public function getPhone()
    {
        return $this->hasMany(OrderDetails::className(),['oid' => 'oid'])->select(['name','phone','oid','reserve_time','certificate','ticket_num']);
    }

    public function getTicket()
    {
        return $this->hasOne(TicketInfo::className(),['tid' => 'tid'])->select(['tid','ticket_sn']);
    }


    /*** 修改订单状态；
     * order_status： 1：待支付，2：已取消，3：已支付，4：已完成，5：订单异常，6：退款中，7：已退款
     * is_finish：1：申请智游宝下单接口正常；2：请求智游宝下单失败
     * @param $oid
     * @return bool
     */
    public function updateOrder($oid)
    {
        $model = self::find()->where('oid = :id',[':id' => $oid])->one();
        if (!is_null($model) and $model->order_status == 3 and $model->is_finish == 1){
            $model->is_finish = '2';
			$model->order_status = 7;
            if ($model->save(0)){
                return true;
            }
         // 只有下单成功，并且门票下单成功的订单才能驳回退款
        }elseif(!is_null($model) and $model->order_status == 3 and $model->is_finish == 2){
            $assistCheckNo = OrderDetails::find()->where(['oid' => $oid])->select(['assist_check_no'])->asArray()->one();
            if ($assistCheckNo['assist_check_no'] != ''){
                $model->is_finish = '1';
                if ($model->save(0)){
                    return true;
                }
            }
        }
        return false;
    }

    public function getTicketInfo($tid)
    {
        $data = self::find()->where('tid = :tid',[':tid' => $tid])->andWhere(['order_status' => 3])->asArray()->one();
        if (!empty($data)){
            return true;
        }
        return false;
    }

    public function RefundOrder($oid)
    {
        $model = self::find()->where('oid = :id',[':id' => $oid])->one();
        if (!is_null($model)){
            $model->order_status = 7;
            $model->pay_note = '手动修改订单状态为已退款';
            if ($model->save(0)){
                return true;
            }
        }
        return false;
    }


    public function getDetail($pay_number,$type = '')
    {
        $query = self::find()->where(['pay_number' => $pay_number]);
        if (!empty($query)){
//            $query->asArray();
        }
        return $query;
    }
}
